import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

// 静态路由对象（默认展示的）
export const constantRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true,
  },

  {
    path: "/404",
    component: () => import("@/views/404"),
    hidden: true,
  },

  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        name: "Dashboard",
        component: () => import("@/views/dashboard/index"),
        meta: { title: "首页", icon: "el-icon-s-home" },
      },
    ],
  },

  // 404 page must be placed at the end !!!
  { path: "*", redirect: "/404", hidden: true },
];

// 动态路由对象
export const dynamicRoutes = [
  {
    path: "/acl",
    component: Layout,
    redirect: "/acl/user",
    name: "Acl",
    meta: { title: "权限管理", icon: "el-icon-s-custom" },
    children: [
      {
        path: "/acl/user",
        name: "User",
        component: () => import("@/views/userCommand/index"),
        meta: { title: "用户管理" },
      },
      {
        path: "/acl/role",
        name: "Role",
        component: () => import("@/views/role/index"),
        meta: { title: "角色管理" },
      },
      {
        path: "/acl/permission",
        name: "Permission",
        component: () => import("@/views/menu/index"),
        meta: { title: "菜单管理" },
      },
    ],
  },

  {
    path: "/product",
    component: Layout,
    name: "Product",
    meta: { title: "商品管理", icon: "form" },
    children: [
      {
        path: "trademark",
        name: "Trademark",
        component: () => import("@/views/brand/index.vue"),
        meta: { title: "品牌管理" },
      },
      {
        path: "attr",
        name: "Attr",
        component: () => import("@/views/platformAttr/index"),
        meta: { title: "平台属性管理" },
      },
      {
        path: "sku",
        name: "Sku",
        component: () => import("@/views/sku/index"),
        meta: { title: "SKU管理" },
      },
      {
        path: "spu",
        name: "Spu",
        component: () => import("@/views/spu/index"),
        meta: { title: "SPU管理" },
      },
    ],
  },

  {
    path: "/test",
    component: Layout,
    name: "test",
    meta: { title: "测试管理", icon: "form" },
    children: [
      {
        path: "testSon",
        name: "testSon",
        component: () => import("@/views/test/testSon/index"),
        meta: { title: "测试子" },
      },
      {
        path: "testSon2",
        name: "testSon2",
        component: () => import("@/views/test/testSon2/index"),
        meta: { title: "测试子2" },
      },
    ],
  },
];

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes,
  });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  // 将之前的路由清空，重新初始化路由器对象
  router.matcher = newRouter.matcher; // reset router
}

export default router;
